package com.wangshupo.app.geekpark.service.impl;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.wangshupo.app.geekpark.model.Author;
import com.wangshupo.app.geekpark.service.AuthorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * 作者服务的实现类
 */
@Service
public class AuthorServiceImpl implements AuthorService {
    private static final Logger LOG = LoggerFactory.getLogger(AuthorServiceImpl.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Author addAuthor(Author author) {
        // 作为服务，要对入参进行判断，不能假设被调用时，传入的一定是真正的对象
        if (author == null) {
            LOG.error("input author data is null.");
            return null;
        }

        if (author.getGmtCreated() == null) {
            author.setGmtCreated(LocalDateTime.now());
        }

        if (author.getGmtModified() == null) {
            author.setGmtModified(LocalDateTime.now());
        }

        return mongoTemplate.insert(author);
    }

    @Override
    public Author getAuthorById(String authorId) {
        // 输入的主键 id 必须有文本，不能为空或全空格
        if (!StringUtils.hasText(authorId)) {
            LOG.error("input authorId is blank.");
            return null;
        }

        Author author = mongoTemplate.findById(authorId, Author.class);
        return author;
    }

    @Override
    public boolean modify(Author author) {
        // 作为服务，要对入参进行判断，不能假设被调用时，入参一定正确
        if (author == null || !StringUtils.hasText(author.getId())) {
            LOG.error("input author data is not correct.");
            return false;
        }

        // 主键不能修改，作为查询条件
        Query query = new Query(Criteria.where("id").is(author.getId()));

        Update updateData = new Update();

        // 每次修改都更新 gmtModified 为当前时间
        updateData.set("gmtModified", LocalDateTime.now());

        // 值为 null 表示不修改。值为长度为 0 的字符串 "" 表示清空此字段
        if (author.getName() != null) {
            updateData.set("name", author.getName());
        }

        if (author.getNick() != null) {
            updateData.set("nick", author.getNick());
        }

        if (author.getAvatar() != null) {
            updateData.set("avatar", author.getAvatar());
        }

        // 把一条符合条件的记录，修改其字段
        UpdateResult result = mongoTemplate.updateFirst(query, updateData, Author.class);
        return result != null && result.getModifiedCount() > 0;
    }

    @Override
    public boolean delete(String authorId) {
        // 输入的主键 id 必须有文本，不能为空或全空格
        if (!StringUtils.hasText(authorId)) {
            LOG.error("input authorId is blank.");
            return false;
        }

        Author author = new Author();
        author.setId(authorId);

        DeleteResult result = mongoTemplate.remove(author);
        return result != null && result.getDeletedCount() > 0;
    }
}
